<template>
  <el-dialog :title="!dataForm.id ?'新增':'修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <!-- <el-form-item label="学生id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="学生id"></el-input>
    </el-form-item> -->
    
    <el-form-item label="学生姓名" prop="userId">
        <el-select v-model="dataForm.userId" filterable :multiple="false"  placeholder="请输入学生姓名" clearable :disabled="dataForm.id">
          <el-option v-for="item in studentList" :key="item.userId" :label="item.realName" :value="item.userId"></el-option>
        </el-select>
      </el-form-item>
    <el-form-item label="数学成绩" prop="maths">
      <el-input v-model.number="dataForm.maths" placeholder="请输入数学成绩"></el-input>
    </el-form-item>
    <el-form-item label="语文成绩" prop="chinese">
      <el-input v-model.number="dataForm.chinese" placeholder="请输入语文成绩"></el-input>
    </el-form-item>
    <el-form-item label="英语成绩" prop="english">
      <el-input v-model.number="dataForm.english" placeholder="请输入英语成绩"></el-input>
    </el-form-item>
    <el-form-item label="考试时间" prop="examTime">
      <el-date-picker v-model="dataForm.examTime" type="datetimerange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" placeholder="选择考试日期时间"></el-date-picker>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible=false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>



<script>
import dayjs from 'dayjs';
  export default{

    data(){
      return{
        visible:false,
        studentList:[],
        dataList:[],
        dataForm:{

          id:0,
          userId:'',

          maths:'',
          chinese:'',
          english:'',
          examTime:[],

          createBy:'',
          createTime:'',

          updateBy:'',
          updateTime:''
        },
        dataRule:{
          userId:[
             {required:true,message:'学生姓名不能为空',trigger:'blur'}
          ],
          maths:[
            {required:true,message:'数学成绩不能为空',trigger:'blur'},
            { type: 'number', message: '数学成绩必须为数字值'},
            { type: 'number', min: 0, max: 100, message: '成绩必须在0到100之间', trigger: 'blur' }
          ],
          chinese:[
            {required:true,message:'语文成绩不能为空',trigger:'blur'},
            { type: 'number', message: '语文成绩必须为数字值'},
            { type: 'number', min: 0, max: 100, message: '成绩必须在0到100之间', trigger: 'blur' }
          ],
          english:[
            {required:true,message:'英语成绩不能为空',trigger:'blur'},
            { type: 'number', message: '英语成绩必须为数字值'},
            { type: 'number', min: 0, max: 100, message: '成绩必须在0到100之间', trigger: 'blur' }
          ],
          examTime:[
            {required:true,message:'考试时间不能为空',trigger:'blur'}
          ]
        }
      }
    },
    methods:{
      init(id,dataList){
        this.dataList=dataList
        this.dataForm.id=id||0
        this.$http({
          url:this.$http.adornUrl('/test/studentScore/studentList'),
          method:'get',
          params:this.$http.adornParams()
        }).then(({data})=>{
          this.studentList=data.studentList
        }).then(() => {
        
        this.visible=true
        this.$nextTick(()=>{
          this.$refs['dataForm'].resetFields()
          if(this.dataForm.id){
            this.$http({
              url:this.$http.adornUrl(`/test/studentScore/info/${this.dataForm.id}`),
              method:`get`,
              params:this.$http.adornParams()
            }).then(({data})=>{
              if(data&&data.code===0){
                console.log(this.studentList);
                this.dataForm.userId=data.studentScore.userId
                this.dataForm.maths=data.studentScore.maths
                this.dataForm.chinese=data.studentScore.chinese
                this.dataForm.english=data.studentScore.english
                this.dataForm.examTime=data.studentScore.examTime.split('至')
                this.dataForm.createBy=data.studentScore.createBy
                this.dataForm.createTime=data.studentScore.createTime
                this.dataForm.updateBy=data.studentScore.updateBy
                this.dataForm.updateTime=data.studentScore.updateTime
              }
            })
          }
        })
      })
      },
      // 表单提交
      dataFormSubmit(){
        this.$refs['dataForm'].validate((valid) =>{
         console.log(this.dataList[1])
        
          if (!this.dataForm.id && this.dataList.some(StudentScoreDTO => StudentScoreDTO.userId === this.dataForm.userId && StudentScoreDTO.examTime === this.dataForm.examTime.map(examTime=>dayjs(examTime).format('YYYY-MM-DD HH:mm:ss')).join('至') )) {
            this.$message({
                  message:'该学生的所在考试时间的成绩信息已经存在，请重新选择时间！',
                  type:'error',
                  duration:1500,
                  onClose:() =>{
                    this.$emit('refreshDataList')
                  }
                })
              }else if (valid){

            this.$http({
              url:this.$http.adornUrl(`/test/studentScore/${!this.dataForm.id?'save':'update'}`),
              method:'post',
              data:this.$http.adornData({
                'id':this.dataForm.id||undefined,
                 'userId':this.dataForm.userId,
                'maths':this.dataForm.maths,
                'chinese':this.dataForm.chinese,
                'english':this.dataForm.english,
                'examTime':this.dataForm.examTime.map(examTime=>dayjs(examTime).format('YYYY-MM-DD HH:mm:ss')).join('至'),
                 'createBy':this.dataForm.createBy,
                 'createTime':this.dataForm.createTime,
                'updateBy':this.dataForm.updateBy,
                'updateTime':this.dataForm.updateTime
              })
            }).then(({data})=>{
              if (data&&data.code===0){
                this.$message({
                  message:'操作成功',
                  type:'success',
                  duration:1500,
                  onClose:() =>{
                    this.visible=false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
